package com.spyx.cart.receive;

import com.rabbitmq.client.Channel;
import com.spyx.cart.service.CartService;
import com.spyx.common.constant.MqConst;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//接收删除购物车商品的消息
@Component
public class CartReceiver {
    @Autowired
    private CartService cartService;

    //创建方法，监听消息的发送--用来监听购物车商品的删除操作
    //@Queue 指定队列信息
    // @Exchange 指定交换机信息
    // key 指定路由键的信息
    //durable  标记是否持久化
    @RabbitListener(
            bindings = @QueueBinding(value =@Queue(value = MqConst.QUEUE_DELETE_CART,durable = "true"),
                    exchange = @Exchange(value = MqConst.EXCHANGE_ORDER_DIRECT),
                    key = {MqConst.ROUTING_DELETE_CART})
    )
    public void deletecart(Long userId, Message message, Channel channel) throws  Exception{
        System.out.println("接收消息-userid="+userId);
        //1.获取消息中的userid
        if(userId!=null){
            cartService.deleteCart(userId);//根据用户id,删除已下单的商品数据
        }
        //2.消息手动确认
        /**
         * 第一个参数：表示收到的消息的标号
         * 第二个参数：如果为true表示可以签收多个消息
         */
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
